package com.javaleague.sesionUsuario;

import com.javaleague.persistencia.DAOLogin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.javaleague.persistencia.DAORegister;
import javax.servlet.http.HttpSession;


public class Register extends HttpServlet {
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String user =  request.getParameter("user");
    	String mail = request.getParameter("email");
    	String pass = request.getParameter("password");
    	String avatar = request.getParameter("avatar");        
			
        if ( DAORegister.writeUserLogin(mail, pass, user, avatar, request.getRemoteAddr()) ) {
            Usuario usuario = DAOLogin.readUserLogin(mail, pass, request.getRemoteAddr());
            HttpSession session = request.getSession(true);
            session.setAttribute( "usuario", usuario);
            response.sendError(200);
        } else
            response.sendError(602);
    }

    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}